//
//  UIGridViewController.m
//  LotteryClient
//
//  Created by Sunyon 14-4-23.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#import "UIGridViewController.h"
#import "TabViewController.h"
#import "TableViewCell.h"

@interface UIGridViewController ()

@end

@implementation UIGridViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    CGSize mSize = [self.view bounds].size;
    CGFloat screenWidth = mSize.width;
    CGFloat screenHeight = mSize.height;
	CGRect frame =  self.view.frame;
	
	if (self.hasNavigationBar)
	{
		frame = CGRectMake(0, self.navigationBarHeight, screenWidth, self.view.height - self.navigationBarHeight - self.customTabViewController.tabBarHeight);
	}
	
    self.tableView = [[UITableView alloc] initWithFrame:frame
                                                   style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor clearColor];
	self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];


	_intervalInCells = 5;
}


#pragma mark UITable datasource and delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger rows = [_delegate numberOfRowsInGridView];
    return rows;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *identifier = @"Cell";

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
	{
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
									  reuseIdentifier:identifier];
        [cell baseSetup];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.backgroundColor = [UIColor clearColor];

	for (UIView *view in cell.subviews)
	{
		if (view.tag == 9999) 
		{
			[view removeFromSuperview];
		}
	}
	
	NSInteger count = [_delegate numberOfColumnInGridView];

	float width = (self.view.frame.size.width - (count +1)*_intervalInCells) / count ;
	float height = [_delegate heightOfRowInGridView:indexPath.row]  ;

	for(int i = 0;i < count; i++)
	{
		UIGridViewCell *gridCell = [_delegate cellInGridViewAtRow:indexPath.row
														   column:i];
        gridCell.isHiddenTitle = self.isHiddenTitle;
		gridCell.backgroundColor = [UIColor clearColor];
		if (gridCell)
		{
			gridCell.frame = CGRectMake(0, 0, width, height - _intervalInCells);
            gridCell.center = CGPointMake((1 + i) * _intervalInCells + width *( 0.5 + i) , _intervalInCells + height * 0.5);
			gridCell.row = indexPath.row;
			gridCell.column = i ;
			gridCell.index = indexPath.row * count + i;
			gridCell.tag = 9999;
			[gridCell addTarget:self
                         action:@selector(cellClick:)
               forControlEvents:UIControlEventTouchUpInside];
			[cell addSubview:gridCell];
		}
	}
//	cell.backgroundColor = [UIColor redColor];
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	float height = [_delegate heightOfRowInGridView:indexPath.row];
	return height;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //不让tableviewcell有选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)cellClick:(UIGridViewCell *)cell
{
	if ([_delegate respondsToSelector:@selector(didSelectedCellInGrid:)])
	{
		[_delegate didSelectedCellInGrid:cell];
	}
}

- (void)reloadSubViews
{
	[_tableView reloadData];
}

@end

